package io.renren.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.dao.DrivingLicenseDao;
import io.renren.dao.VehicleDao;
import io.renren.dao.VioCodewfdmDao;
import io.renren.dao.VioSurveilDao;
import io.renren.dao.VioViolationDao;
import io.renren.datasources.DataSourceNames;
import io.renren.datasources.annotation.DataSource;
import io.renren.entity.DrivingLicense;
import io.renren.entity.Vehicle;
import io.renren.entity.VioCodewfdm;
import io.renren.entity.VioSurveil;
import io.renren.entity.VioViolation;
import io.renren.service.DataInfoQueryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author rdawnx
 * @package_name io.renren.service.impl
 * @class_name RegCheckServiceImpl.java
 * @author_email rdawnxhi@163.com
 * @JDK_version 1.8
 * @create_on 2019/06/25 12:00
 */
@Service
public class DataInfoQueryServiceImpl implements DataInfoQueryService {

    /**
     * 是否调用视频网数据,true表示调用，false表示不调用
     */
    private boolean isRealCall = true;

    @Autowired
    private VehicleDao vehicleDao;

    @Autowired
    private DrivingLicenseDao drivingLicenseDao;

    @Autowired
    private VioViolationDao vioViolationDao;

    @Autowired
    private VioCodewfdmDao vioCodewfdmDao;

    @Autowired
    private VioSurveilDao vioSurveilDao;

    /**
     * 根据参数查询车辆信息列表
     *
     * @param hpzl 号牌种类
     * @param hphm 号牌号码
     * @param fdjh 发动机号
     * @return 列表
     */
    @DataSource(name = DataSourceNames.SECOND)
    @Override
    public List<Vehicle> getVehicleInfoByParams(String hpzl, String hphm, String fdjh) {
        if (!isRealCall) {
            return null;
        }
        // 号牌种类和号牌号码是必填
        if (StringUtils.isBlank(hpzl) || StringUtils.isBlank(hphm)) {
            return Collections.emptyList();
        }
        if (StringUtils.isBlank(fdjh)) {
            fdjh = "";
        }
        return vehicleDao.selectListByParams(hpzl, hphm, fdjh);
    }

    /**
     * 根据实体参数获取实体类对象列表
     *
     * @param drivingLicense 参数
     * @return 返回实体对象列表
     */
    @DataSource(name = DataSourceNames.SECOND)
    @Override
    public List<DrivingLicense> getDrivingLicenseInfo(DrivingLicense drivingLicense) {
        if (!isRealCall) {
            return null;
        }
        return drivingLicenseDao.selectList(new EntityWrapper<>(drivingLicense));
    }

    /**
     * 根据实体参数获取实体类对象列表
     *
     * @param vioViolation 参数
     * @return 返回实体对象列表
     */
    @DataSource(name = DataSourceNames.SECOND)
    @Override
    public List<VioViolation> getVioViolationInfo(VioViolation vioViolation) {
        if (!isRealCall) {
            return null;
        }
        return vioViolationDao.selectList(new EntityWrapper<>(vioViolation));
    }

    /**
     * 根据实体参数获取实体类对象列表
     *
     * @param vioCodewfdm 参数
     * @return 返回实体对象列表
     */
    @DataSource(name = DataSourceNames.SECOND)
    @Override
    public List<VioCodewfdm> getVioCodewfdmInfo(VioCodewfdm vioCodewfdm) {
        if (!isRealCall) {
            return null;
        }
        return vioCodewfdmDao.selectList(new EntityWrapper<>(vioCodewfdm));
    }

    /**
     * 根据实体参数获取实体类对象列表
     *
     * @param vioSurveil 参数
     * @return 返回实体对象列表
     */
    @DataSource(name = DataSourceNames.SECOND)
    @Override
    public List<VioSurveil> getVioSurveilInfo(VioSurveil vioSurveil) {
        if (!isRealCall) {
            return null;
        }
        EntityWrapper<VioSurveil> ew = new EntityWrapper<>(vioSurveil);
        ew.orderBy("WFSJ",false);
        return vioSurveilDao.selectList(ew);
    }

    /**
     * 检查用户的姓名身份证号是否一致
     *
     * @param name     姓名
     * @param idNumber 身份证号
     * @return 检查结果：true表示通过校验，false表示未通过
     */
    @DataSource(name = DataSourceNames.SECOND)
    @Override
    public boolean checkNameAndIDCard(String name, String idNumber) {
        if (!isRealCall) {
            return false;
        }
        // 查询车辆表
        EntityWrapper entityWrapper = new EntityWrapper(new Vehicle());
        entityWrapper.eq("SYR", name).eq("SFZMHM", idNumber);
        int vehicleCount = vehicleDao.selectCount(entityWrapper);
        if (vehicleCount > 0) {
            return true;
        }
        // 查询驾照表
        entityWrapper = new EntityWrapper(new DrivingLicense());
        entityWrapper.eq("XM", name).eq("SFZMHM", idNumber);
        int drivingLicenseCount = drivingLicenseDao.selectCount(entityWrapper);
        return drivingLicenseCount > 0;
    }
}
